package com.okchan.web;

import com.okchan.domain.Book;
import com.okchan.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Ok chan
 */
@RestController
//Spring4之后新加入的注解，原来返回json需要@ResponseBody和@Controller配合.(组合注解)
@RequestMapping("web/book")
public class BookController {

    //@Autowired由Spring框架定义，用于描述类中的属性后者方法（构造方法，set方法）
    @Autowired
    private BookService bookService;

    //@GetMapping注解： GET请求：从服务器获取特定资源。  @GetMapping("/blog")相当于@RequestMapping(value="/blog",method=RequestMethod.GET)
    //查询图书
    @GetMapping("/get")
    public Book get(@RequestParam(value = "book_id", required = false) final String bookId){
        return bookService.selectBookById(bookId);
    }

    //查询所有图书
    @RequestMapping("/bookAll")
    public List<Book> bookAll(){
        List<Book> list=bookService.booAll();
        return list;
    }

    //@PostMapping注解：POST请求：在服务器上创建一个新的资源。
    //@PostMapping("/blogs/save")等价于@RequestMapping(value="/blog/save",method=RequestMethod.POST)
    //@RequestBody注解：用于读取Request请求的body部分，并且ContentType为application/json格式的数据，接收到数据后会自动将数据绑定到Java对象上
    //添加图书
    @PostMapping("/post")
    public Book post(@RequestBody Book book) {    //@RequestBody注解用于接收前端传递給后端的，JSON对象的字符串
        return this.bookService.createBook(book);
    }

    //删除图书
    @PostMapping("/delete")
    public Book delete(@RequestBody Book bookId) {
        return this.bookService.deleteBookId(bookId);
    }

    //更新图书
    @PostMapping("/update")
    public Book update(@RequestBody Book bookName) {
        return this.bookService.updateBook(bookName);
    }

}



